<template>
    <div class="app-container">

        <el-row :gutter="10" class="app-table-top-action ">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="handleDelete(ids)">删除</el-button>
            </el-col>
        </el-row>

        <el-table  @selection-change="handleSelectionChange" v-loading="listLoading" :data="list"
            element-loading-text="Loading" border fit highlight-current-row>
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column align="center" label="#" width="95">
                <template slot-scope="scope">
                    {{ scope.row.id }}
                </template>
            </el-table-column>

            <el-table-column class-name="status-col" label="发送人" width="180" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.sender_name }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column class-name="status-col" label="收信人" width="180" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.receiver_name }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column class-name="status-col" label="内容" width="330" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.content }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column class-name="status-col" label="是否已读" width="110" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ scope.row.is_read == 0 ? "未读" : "已读" }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column class-name="status-col" label="发送时间" width="220" align="center">
                <template slot-scope="scope">
                    <el-tag>{{ dayjs(scope.row.create_time).format("YYYY-MM-DD HH:mm:ss") }}</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete"
                        @click="handleDelete([scope.row.id])">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                :page-sizes="[10, 20, 50, 100]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <el-dialog title="添加" :visible.sync="addV" width="600px" top="20px">
            <el-form ref="addForm" :model="addForm" label-width="100px">
                <el-form-item label="发送人" prop="sender">
                    <el-select v-model="addForm.sender">
                        <el-option v-for="(v,i) in user_list" :key="i" :label="v.nickname" :value="v.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="收信人" prop="receiver">
                    <el-select v-model="addForm.receiver">
                        <el-option v-for="(v,i) in user_list" :key="i" :label="v.nickname" :value="v.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="消息内容" prop="content">
                    <el-input v-model="addForm.content" type="textarea"/>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addV = false">取 消</el-button>
                <el-button type="primary" @click="doAdd()">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="修改" :visible.sync="editV" width="600px" top="20px">
            <el-form ref="editForm" :model="editForm" label-width="100px">
                <el-form-item label="发送人" prop="sender">
                    <el-select v-model="editForm.sender">
                        <el-option v-for="(v,i) in user_list" :key="i" :label="v.nickname" :value="v.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="收信人" prop="receiver">
                    <el-select v-model="editForm.receiver">
                        <el-option v-for="(v,i) in user_list" :key="i" :label="v.nickname" :value="v.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="消息内容" prop="content">
                    <el-input v-model="editForm.content" type="textarea"/>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editV = false">取 消</el-button>
                <el-button type="primary" @click="doEdit()">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>
<script>
import api from "@/api/msg/msg";
import dayjs from "dayjs";
import publicApi from "@/api/public";

export default {
    name: "msg-msg-list",
    data() {
        return {
            list: null,
            listLoading: false,
            page: 1,
            limit: 10,
            total: 0,
            ids: [],
            addV: false,
            addForm: {},
            editV: false,
            editForm: {},
            dayjs:dayjs,
            user_list:[]
        }
    },
    created() {
        this.initialize();

    },
    methods: {
        async listAllUser(){
            let res  = await publicApi.listAllUser();
            this.user_list = [...res.data];
        },

        handleSizeChange(val) {
            this.page = 1;
            this.limit = val;
            this.initialize();
        },
        handleCurrentChange(val) {
            this.page = val;
            this.initialize();
        },
        async initialize() {
            let sdata = {
                page: this.page,
                limit: this.limit
            }
            let res = await api.list(sdata);
            this.total = res.count;
            this.list = [...res.data];
        },
        handleUpdate(val) {
            this.listAllUser();

            let temp = { ...val };
            this.editForm = {
                id: temp.id,
                sender: temp.sender,
                receiver: temp.receiver,
                content: temp.content,
                create_time:temp.create_time
            };
            this.editV = true;
        },
        handleDelete(val) {
            this.$confirm('真的要删除吗, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                let res = await api.delete(val);
                if (res.code == 0) {
                    this.$message({
                        type: 'success',
                        message: res.msg
                    });
                } else {
                    this.$message({
                        type: 'error',
                        message: res.msg
                    });
                }
                this.initialize();

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handleSelectionChange(selection) {
            let temp = [];
            selection.map(x => {
                temp.push(parseInt(x.id));
            });
            this.ids = [...temp];
        },
        handleAdd() {
            this.listAllUser();
            this.addV = true;
        },
        async doAdd() {
            let sdata = { ...this.addForm };
            let res = await api.add(sdata);
            if (res.code == 0) {
                this.$message.success(res.msg);
                this.initialize();
            }
        },
        async doEdit() {
            let sdata = { ...this.editForm };
            let res = await api.edit(sdata);
            if (res.code == 0) {
                this.$message.success(res.msg);
                this.initialize();
                this.editV = false;
            }
        }
    }
}
</script>